#include <iostream>
using namespace std;

//例子一：饿汉式（线程安全）
class Hungry
{
public:
	static Hungry* getInstance(){return &hungry_;}
	void hello(){cout<<"Hungry pattern!"<<endl;}
private:
	static Hungry hungry_;
	Hungry(){}
	Hungry(const Hungry&) = delete;
	Hungry& operator=(const Hungry&) = delete;
};
Hungry Hungry::hungry_;

//例子二：懒汉式（线程安全）
class Lazy
{
public:
	static Lazy& getInstance()
	{
		//此局部变量会在第一次调用的时候
		static Lazy lazy;
		return lazy;
	}
	void hello(){cout<<"Lazy pattern!"<<endl;}
private:
	Lazy(){}
	Lazy(const Lazy&) = delete;
	Lazy& operator=(const Lazy&) = delete;
};

//例子三：懒汉式（线程不安全）
class Lazy2
{
public:
	static Lazy2* getInstance()
	{
		if(nullptr == lazy_)
		{
			lazy_ = new Lazy2();
		}
		return lazy_;
	}
	void hello(){cout<<"Lazy2 pattern!"<<endl;}
private:
	static Lazy2* lazy_;
	Lazy2(){}
	Lazy2(const Lazy2&) = delete;
	Lazy2& operator=(const Lazy2&) = delete;
};
Lazy2* Lazy2::lazy_ = nullptr;

int main()
{
	Hungry::getInstance()->hello();
	Lazy::getInstance().hello();
	Lazy2::getInstance()->hello();
	return 0;
}
